package com.yangchuang.openpicture.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yangchuang.openpicture.exception.BussinessException;
import com.yangchuang.openpicture.mapper.CommentMapper;
import com.yangchuang.openpicture.pojo.Comment;
import com.yangchuang.openpicture.service.CommentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 评论服务实现类
 *
 * @author naiyi
 * @since 2025-09-01
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    private final CommentMapper commentMapper;

    @Override
    public Comment addComment(Comment comment) {
        // 设置评论层级
        if (comment.getParentId() == null) {
            // 顶级评论
            comment.setLevel(1);
        } else {
            // 嵌套回复，先获取父评论
            Comment parentComment = commentMapper.selectById(comment.getParentId());
            if (parentComment == null) {
                throw new BussinessException(null);
            }
            // 目前只支持二级回复
            comment.setLevel(2);
        }

        // 设置初始点赞数和不喜欢数
        comment.setLikeCount(0);
        comment.setDislikeCount(0);

        // 设置创建时间和更新时间
        Date now = new Date();
        comment.setCreateTime(now);
        comment.setUpdateTime(now);

        // 保存评论
        commentMapper.insert(comment);
        return comment;
    }

    @Override
    public List<Comment> getCommentsByPictureId(Long pictureId) {
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getPictureId, pictureId)
                .orderByAsc(Comment::getCreateTime);
        return commentMapper.selectList(wrapper);
    }

    @Override
    public boolean deleteComment(Long commentId, Long userId) {
        Comment comment = commentMapper.selectById(commentId);
        if (comment == null) {
            throw new BussinessException(null);
        }

        // 验证权限：只有评论作者或管理员可以删除评论
        if (!comment.getUserId().equals(userId)) {
            throw new BussinessException(null);
        }

        // 逻辑删除
        return commentMapper.deleteById(commentId) > 0;
    }

    @Override
    public boolean likeComment(Long commentId, Long userId) {
        Comment comment = commentMapper.selectById(commentId);
        if (comment == null) {
            throw new BussinessException(null);
        }

        // 增加点赞数
        comment.setLikeCount(comment.getLikeCount() + 1);
        comment.setUpdateTime(new Date());

        return commentMapper.updateById(comment) > 0;
    }

    @Override
    public boolean unlikeComment(Long commentId, Long userId) {
        Comment comment = commentMapper.selectById(commentId);
        if (comment == null) {
            throw new BussinessException(null);
        }

        // 减少点赞数（确保不会小于0）
        if (comment.getLikeCount() > 0) {
            comment.setLikeCount(comment.getLikeCount() - 1);
            comment.setUpdateTime(new Date());
            return commentMapper.updateById(comment) > 0;
        }

        return true;
    }
}
